package org.libarae.communication.response;

import java.io.ByteArrayOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.libarae.communication.HTTPUtils;
import org.libarae.communication.XmlParseException;
import org.libarae.communication.common.Type;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class GenericResponse extends Response{
	//must be greater than 2
	public static final int STATUS_FAILED_UNKNOWN_REQUEST = 3;
	public static final int STATUS_FAILED_CHANGE_ME = 4;
	
	public GenericResponse(int status){
		super(Type.GENERIC, status);		
	}

	@Override
	public String createXML() throws XmlParseException {
		Document doc;
		try{
			//setup the factory to build the xml
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			doc = documentBuilder.newDocument();
		}
		catch (Exception e) {
			throw new XmlParseException("Error creating XML Builder",e);
		}
		
		//create the xml
		Element rootElement = doc.createElement("type");
		rootElement.setAttribute("typeID", super.getType()+"");
		doc.appendChild(rootElement);
		//user name
		Element node = doc.createElement("mStatus");
		node.setTextContent(getStatus()+"");
		rootElement.appendChild(node);
		
		//export the xml to a string
		String output = null;
		try {
			ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(byteArrayOutput);
			transformer.transform(source, result);
			output = byteArrayOutput.toString();
		}
		catch (Exception e) {
			throw new XmlParseException("Error outputting XML to string", e);
		}
		
		//return the string
		return output;
	}

	@Override
	public void decodeXML(Document document) {
		// get the document root and node list
		Element root = document.getDocumentElement();
		NodeList nodeList = root.getChildNodes();

		// set all of the nodes in their proper places
		for (int i = 0; i < nodeList.getLength(); i++) {
			// find what node we have
			String nodeName = nodeList.item(i).getNodeName();
			if (nodeName.equals("mStatus")) {
				setStatus(Integer.parseInt(nodeList.item(i).getTextContent()));
			}
			else {
				System.err.println("Unknow field in object XML: " + nodeName);
			}
		}	
	}

	@Override
	public String createHTML() {
		if(getStatus() == Response.STATUS_FAILED_BAD_KEY)
		{
			return HTTPUtils.HTMLDefultHeader()+"<head><meta http-equiv=\"REFRESH\" content=\"3;url=/\"></head><center><br/><h3>You've Been Logged Out Due to Inactivity</h3></br><a class=\"nounderline\" href=\"/\">Please Login Again</a>.</center>";
		}
		else if(getStatus() == Response.STATUS_FAILED_GENERIC)
		{
			return "<h2>Sorry, something bad happened</h2> </br> Please report what happend! Trying going to the previous page" ;
		}
		else if(getStatus() == Response.STATUS_GOOD)//Status may be good, but they shouldent be getting this response! 
		{
			return HTTPUtils.HTMLPageHeader("") + "<h2>Sorry, something bad happened</h2> </br> Please report what happend! Trying going to the previous page" ;
		}
		
		//Should not get here, only i case of new status
		return null;
	}
}
